﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class Support_CellInfo : BasicPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetPager();
        }
    }

    private void SetPager()
    {
        PagedDataSource pds = new PagedDataSource();
        DataSet ds = GetCell();
        CellPager.RecordCount = ds.Tables[0].Rows.Count;
        pds.DataSource = ds.Tables[0].DefaultView;
        pds.AllowPaging = true;
        pds.PageSize = CellPager.PageSize;
        pds.CurrentPageIndex = CellPager.CurrentPageIndex - 1;
        CellInfo.DataSource = pds;
        CellInfo.DataBind();
    }

    private DataSet GetCell()
    {
        string sqlstr = @"SELECT CELL_CHNAME,CELL_ENNAME,CELL_CLASS,CELL_CLASSIFICATION AS CELL_NET,CELL_NUMBER
                            FROM CELL_CLASS ";
        if (CellInfo_I.Text.Trim().Length > 0)
        {
            sqlstr = sqlstr + " WHERE CELL_CHNAME LIKE '%" + ReplaceString(CellInfo_I.Text.Trim(),100) + "%'";
        }
        sqlstr = sqlstr + " ORDER BY CELL_CHNAME ";
        return OracleHelper.ExecuteDataSet(sqlstr, CommandType.Text, null);
    }

    protected void CellPager_PageChanged(object sender, EventArgs e)
    {
        SetPager();
    }

    protected void CellInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        CellInfo.EditIndex = e.NewEditIndex;
        SetPager();
    }

    protected void CellInfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        CellInfo.EditIndex = -1;
        SetPager();
    }

    protected void CellInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string sqlstr = "AB_CELLINFO_P";
        OracleParameter[] _params = {
                new OracleParameter("V_CELL",OracleType.NVarChar,100),
                new OracleParameter("v_CLASS",OracleType.NVarChar,20),
                new OracleParameter("v_NET",OracleType.NVarChar,20),
                new OracleParameter("V_NUMBER",OracleType.VarChar,10),
                new OracleParameter("V_ENNAME",OracleType.VarChar,100)
            };
        _params[0].Value = CellInfo.DataKeys[e.RowIndex].Value;
        DropDownList ddl1 = CellInfo.Rows[e.RowIndex].FindControl("CELL_CLASS_D") as DropDownList;
        DropDownList ddl2 = CellInfo.Rows[e.RowIndex].FindControl("CELL_NET_D") as DropDownList;
        TextBox tx = CellInfo.Rows[e.RowIndex].FindControl("CELL_NUMBER_T") as TextBox;
        TextBox tx2 = CellInfo.Rows[e.RowIndex].FindControl("CELL_ENNAME_T") as TextBox;
        _params[1].Value = ddl1.SelectedValue;
        _params[2].Value = ddl2.SelectedValue;
        _params[3].Value = tx.Text.Trim();
        _params[4].Value = tx2.Text.Trim();
        OracleHelper.ExecuteNonQuery(sqlstr, CommandType.StoredProcedure, _params);
        CellInfo.EditIndex = -1;
        SetPager();
    }

    protected void CellInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
        {
            if (e.Row.FindControl("CELL_CLASS_D") != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("CELL_CLASS_D");
                ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "CELL_CLASS").ToString();
            }
            if (e.Row.FindControl("CELL_NET_D") != null)
            {
                DropDownList ddl = (DropDownList)e.Row.FindControl("CELL_NET_D");
                ddl.SelectedValue = DataBinder.Eval(e.Row.DataItem, "CELL_NET").ToString();
            }
        }
    }
    protected void QueryUser_Click(object sender, EventArgs e)
    {
        SetPager();
    }
    protected void BtnCreate_Click(object sender, EventArgs e)
    {
        if (InvalidCell())
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "sysinfo"
                , "$(function(){$('#AddCellDiv').dialog('open');alert('基站名称已存在');});", true);
            return;
        }
        string sqlstr = "AB_CELLINFO_P";
        OracleParameter[] _params = {
                new OracleParameter("V_CELL",OracleType.NVarChar,100),
                new OracleParameter("v_CLASS",OracleType.NVarChar,20),
                new OracleParameter("v_NET",OracleType.NVarChar,20),
                new OracleParameter("V_NUMBER",OracleType.VarChar,10),
                new OracleParameter("V_ENNAME",OracleType.VarChar,100)
            };
        _params[0].Value = CellInfo_A.Text.Trim();
        _params[1].Value = CellClass_A.SelectedValue;
        _params[2].Value = CellNet_A.SelectedValue;
        _params[3].Value = CellNumber_A.Text.Trim();
        _params[4].Value = CellEnInfo_A.Text.Trim();
        int count = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.StoredProcedure, _params);
        SetPager();
        if (count > 0)
        {
            JQueryAlert("基站新增成功！");
        }
    }

    private bool InvalidCell()
    {
        string sqlstr = "Select count(1) from cell_class where cell_chname = :cell_chnamme";
        OracleParameter[] _params = {
                new OracleParameter(":cell_chnamme",OracleType.NVarChar,100)
            };
        _params[0].Value = CellInfo_A.Text.Trim();
        int res = Int32.Parse(OracleHelper.ExecuteScalar(sqlstr, CommandType.Text, _params).ToString());
        return res > 0;
    }

    protected void CellInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "DELETE FROM CELL_CLASS WHERE CELL_CHNAME = :CELL_CHNAME";
        OracleParameter[] _params = {
                new OracleParameter(":CELL_CHNAME",OracleType.NVarChar,100)
            };
        _params[0].Value = CellInfo.DataKeys[e.RowIndex].Value;
        int count = OracleHelper.ExecuteNonQuery(sqlstr, CommandType.Text, _params);
        SetPager();
        if (count > 0)
        {
            JQueryAlert("基站删除成功!");
        }
    }
}
